<?php

/**
 * O2O团购PC版本
 * ============================================================================
 * * 版权所有 2009-2016 郑州司科计算机科技有限公司，并保留所有权利。
 * 购买网址: http://www.jzphp.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
  * 咨询电话：400-823-0371 QQ：313999875
 * ============================================================================
 * $Author: zzsike $
 * $Time 2016-02-03 sammy $
*/


require_once(dirname(dirname(dirname(__FILE__))) . '/app.php');

need_manager();
need_auth('order');
$ptime=time()+1;
$rep_id= abs(intval($_GET['rep_id']));
if($rep_id){
          $repakage = Table::Fetch('order',$rep_id);
          if($repakage['flag']&&$repakage['packageid']>0)
          {
             $rconditionw = array(
	                'state' => 'pay',
					'express' => 'Y',
	                'team_id > 0',
	                "delivery_time < {$ptime} OR delaysend = 'N'",
	                'flag > 0',
	                 'express_id <=0',
                    'user_id'=>$repakage['user_id'],
                      );
             $restorders = DB::LimitQuery('order', array(
	              'condition' => $rconditionw,
	               'order' => 'ORDER BY  id DESC',
                   ));	
             foreach($restorders AS $two)
                    {
                     Table::UpdateCache('order',$two['id'], array( 'flag' =>0,'packageid' =>'-1',));
                     }
            }
            }

$condition = array(
	'state' => 'pay',
	'express' => 'Y',
	'team_id > 0',
	"delivery_time < {$ptime} OR delaysend = 'N'",
	'packageid<0',
	'express_id <=0',
     );
//建立旗帜
$oneflage = DB::LimitQuery('order', array(
	'condition' => $condition,
	'one' => true,));
if (!$oneflage['id'])
{
$orderwate_count = Table::Count('order',array(
	"packageid > 0",
	'express' => 'Y',
	'state' => 'pay',
	"delivery_time < {$ptime} OR delaysend = 'N'",
	'team_id > 0',
	'express_id' =>0,
	"flag >0",));
	if($orderwate_count){
     Session::Set('error', "订单已经打包整合完毕，部分订单需要人工审核整合！");
     redirect( WEB_ROOT . "/manage/goods/edit.php");
        }
     else
       {
     Session::Set('notice', "所有订单已经打包整合完毕，可以发货了！");
     redirect( WEB_ROOT . "/manage/goods/send.php");
       }
die();
}
$conditionw = array(
	'state' => 'pay',
	'express' => 'Y',
	"delivery_time < {$ptime} OR delaysend = 'N'",
	'team_id > 0',
	'packageid'=>'-1',
	'express_id' =>0,
    'user_id'=>$oneflage['user_id'],
          );
//寻找旗帜用户的未发货订单
$ordersw = DB::LimitQuery('order', array(
	'condition' => $conditionw,
	'order' => 'ORDER BY  id DESC',
));	


//建立包裹
$pakageinfo=array();
 $pakageinfo['flage_id']=0;
 $packageflag = new Table('package', $pakageinfo);
	$insertf = array('flage_id');
$pflagid= $packageflag->update($insertf);
//订单放入包裹中
$package_c=array();
$ids=array();
foreach($ordersw AS $one)
{
      if($one['realname']!=$oneflage['realname']||$one['address']!=$oneflage['address'])
             Table::UpdateCache('order',$one['id'], array( 'packageid' =>$pflagid, 'flag' =>$oneflage['id'],)); 
      else{
           if($one['team_id']==9999999)
               {
			   $title=$title.'<font color="#FF0000">购物车'.$one['id'].'</font>：';
               $cartinf = Utility::ExtraDecode($one['cartinf']);
                foreach($cartinf as $key=>$value)
                           {
                           array_unshift($ids,$key);
                           $teamuut = Table::Fetch('team', $key);
						   $title=$title.$teamuut['product']."[".$value."件]&nbsp;";
						   $quantity=$quantity+$value;
                           }
						  $title=$title.'<br>'; 
               }
          else
              {
                array_unshift($ids,$one['team_id']);
                $onesteam = Table::Fetch('team', $one['team_id']);
                $title=$title.'<font color="#FF0000">订单号'.$one['id'].'</font>：'.$onesteam['product']."[".$one['quantity']."件]<br>";
                $quantity=$quantity+$one['quantity'];
			  }
        if($one['remark'])
        $remark=$remark.'订单号'.$one['id']."备注：".$one['remark'].'<br>';
               {
        $origin=$origin+$one['origin'];


}
Table::UpdateCache('order',$one['id'], array( 'packageid' =>$pflagid,));
}
}
 $ids=array_unique($ids);
 $package_c['team_id']= Utility::ExtraEncode($ids);
 $package_c['origin']=$origin;
 $package_c['quantity']=$quantity;
 $package_c['title']=$title;
  $package_c['remark']=$remark;
  $package_c['user_id']=$oneflage['user_id'];
  $package_c['realname']=$oneflage['realname'];
  $package_c['mobile']=$oneflage['mobile'];
  $package_c['zipcode']=$oneflage['zipcode'];
  $package_c['address']=$oneflage['address'];
  $package_c['express_xx']=$oneflage['express_xx'];
  $package_c['condbuy']=$oneflage['condbuy'];
 DB::Update('package',$pflagid,$package_c);

 redirect( WEB_ROOT . "/manage/goods/packageing.php");


